<script lang="ts" setup>
import PageDialog from "@/components/PageDialog/index.vue";
import {ref} from "vue";
import {ProjectObj, StationObj} from "@/api/demoClass";
import {addProject, addStation, editStation, projectList} from "@/api/demoApi";
import {message} from "@/utils/message";
import {ElMessageBox} from "element-plus";

defineOptions({
  name: "stationBeforeData"
});

/**-------------------表单相关-------------------**/
//表单参数
const formData = ref({
  id: null,
  stationNo:null,
  planName:null,
  isNew:0,
  versions:0,
  area:null,
  operator:null,
  projectId:null,
  projectName:null,
  address:null,
  signingName: null,
  siteNo: null,
  status: null
})

//表单校验
const formRules = ref({
  stationNo:[{required:true,message:'请输入更新后编号',trigger:'blur'}],
  planName:[{required:true,message:'请输入规划(建设)站名',trigger:'blur'}],
  signingName:[{required:true,message:'请输入签约后站名',trigger:'blur'}],
  siteNo:[{required:true,message:'请输入物理站址编号',trigger:'blur'}],
  isNew:[{required:true,message:'请选择是否新展',trigger:'change'}],
  versions:[{required:true,message:'请选择新老项目',trigger:'change'}],
  area:[{required:true,message:'请选择所属区域',trigger:'change'}],
  operator:[{required:true,message:'请选择运营商',trigger:'change'}],
  projectId:[{required:true,message:'请选择所属项目',trigger:'change'}],
})

//业主选项
let projectOptions = projectList().map(item=>({value:item.id,label:`${item.projectNo}(${item.projectName})`}))
const isNewOptions = [
  {value:0,label:'否'},
  {value:1,label:'是'},
]
const versionOptions = [
  {value:0,label:'新项目'},
  {value:1,label:'老项目'},
]
const operatorOptions = [
  {value:0,label:'移动'},
  {value:1,label:'联通'},
  {value:2,label:'电信'},
]
const areaOptions = [
  {value:0,label:'西湖区'},
  {value:1,label:'拱墅区'},
  {value:2,label:'上城区'},
  {value:3,label:'临平区'},
]

/**-------------------打开弹框-------------------**/
//标题
const dialogTitle = ref('')
const dialog = ref()
//弹框显示
const dialogShow = (title: string,data?: object)=>{
  dialogTitle.value = title
  if(data) {
    formData.value = {...data}
  }
  dialog.value.dialogVisible = true
}
defineExpose({
  dialogShow
})

/**-------------------事件回调-------------------**/
//取消
const form = ref()
const handlerCancel = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.resetFields()
    formData.value = {
      id: null,
      stationNo:null,
      planName:null,
      isNew:null,
      versions:null,
      area:null,
      operator:null,
      projectId:null,
      projectName:null,
      address:null,
      signingName: null,
      siteNo: null,
      status: null
    }
    dialog.value.dialogVisible = false
    resolve({})
  }catch(e){
    console.log(e)
    reject(e)
  }
})
//确认
const emits = defineEmits(['done'])
const handlerConfirm = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.validate()
    editStation(formData.value)
    message("编辑成功", { type: "success" });
    await handlerCancel()
    emits('done')
    resolve({})
  }catch(e){
    reject(e)
  }
})
</script>

<template>
  <PageDialog ref="dialog" :handler-cancel="handlerCancel" :handler-confirm="handlerConfirm" :title="dialogTitle" width="55%">
    <el-form ref="form" :model="formData" :rules="formRules" label-position="top" label-width="auto">
      <el-form-item label="更新后编号" prop="stationNo">
        <el-input v-model="formData.stationNo" clearable disabled placeholder="请输入更新后编号" />
      </el-form-item>
      <el-form-item label="规划(建设)站名" prop="planName">
        <el-input v-model="formData.planName" clearable placeholder="请输入规划(建设)站名" />
      </el-form-item>
      <el-form-item label="签约后站名" prop="signingName">
        <el-input v-model="formData.signingName" clearable placeholder="请输入签约后站名" />
      </el-form-item>
      <el-form-item label="物理站址编号" prop="siteNo">
        <el-input v-model="formData.siteNo" clearable placeholder="请输入物理站址编号" />
      </el-form-item>
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="formData.projectId" clearable disabled filterable placeholder="请选择所属项目">
          <el-option v-for="(item,index) in projectOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否新展" prop="isNew">
        <el-select v-model="formData.isNew" clearable disabled filterable placeholder="请选择是否新展">
          <el-option v-for="(item,index) in isNewOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="新老项目" prop="versions">
        <el-select v-model="formData.versions" clearable disabled filterable placeholder="请选择新老项目">
          <el-option v-for="(item,index) in versionOptions" :key="index" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属区域" prop="area">
        <el-select v-model="formData.area" clearable disabled filterable placeholder="请选择所属区域">
          <el-option v-for="(item,index) in areaOptions" :key="index" :label="item.label" :value="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="运营商" prop="operator">
        <el-select v-model="formData.operator" clearable disabled filterable placeholder="请选择运营商">
          <el-option v-for="(item,index) in operatorOptions" :key="index" :label="item.label" :value="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="formData.address" clearable placeholder="请输入详细地址" />
      </el-form-item>
    </el-form>
  </PageDialog>
</template>

<style lang="scss" scoped>
:deep(.el-form) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;

  .el-form-item {
    width: 48%;

    .el-input,
    .el-select {
      width: 100%;
    }
  }
}
</style>
